<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-form
          :label-position="labelPosition"
          label-width="100px"
          ref="ruleForm"
        >
          <el-row>
            <el-col class="top_title">
              <span>说明：目前支持学科和关键字条件筛选</span>
              <el-button
                type="success"
                size="small"
                icon="el-icon-edit"
                @click="toAddQuestion"
                >新增试题</el-button
              >
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="学科">
                <el-select
                  v-model="form.subjectID"
                  placeholder="请选择"
                  size="small"
                  @change="changeSelect"
                >
                  <el-option
                    v-for="item in subject"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录">
                <el-select
                  v-model="form.catalogID"
                  placeholder="请选择"
                  size="small"
                >
                  <el-option
                    v-for="item in directorys"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签">
                <el-select
                  v-model="form.tags"
                  placeholder="请选择"
                  size="small"
                >
                  <el-option
                    v-for="item in labelTags"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字">
                <el-input
                  placeholder="根据题干搜索"
                  v-model="form.keyword"
                  class="shuru"
                  size="small"
                ></el-input>
              </el-form-item>
            </el-col>
            </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="试题类型">
                <el-select
                  v-model="form.questionType"
                  placeholder="请选择"
                  size="small"
                >
                  <el-option
                    v-for="item in questionType"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="难度">
                <el-select
                  v-model="form.difficulty"
                  placeholder="请选择"
                  size="small"
                >
                  <el-option
                    v-for="item in difficulty"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="方向">
                <el-select
                  v-model="form.direction"
                  placeholder="请选择"
                  size="small"
                >
                  <el-option
                    v-for="item in direction"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="录入人" size="small">
                <el-select
                  v-model="form.creatorID"
                  placeholder="请选择"
                  size="small"
                >
                  <el-option
                    v-for="item in userInfor"
                    :key="item.id"
                    :label="item.username"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="题目标注">
                <el-input
                  v-model="form.remarks"
                  class="shuru"
                  size="small"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企业简称">
                <el-input
                  v-model="form.shortName"
                  class="shuru"
                  size="small"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="城市">
                <el-row type="flex" class="row-bg" justify="space-between">
                  <el-col :span="11">
                    <el-select
                      v-model="form.province"
                      placeholder="请选择"
                      size="small"
                      @change="cityChange"
                    >
                      <el-option
                        v-for="item in provinces"
                        :key="item"
                        :label="item"
                        :value="item"
                      ></el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="11">
                    <el-select
                      v-model="form.city"
                      placeholder="请选择"
                      size="small"
                    >
                      <el-option
                        v-for="item in cityList"
                        :key="item"
                        :label="item"
                        :value="item"
                      ></el-option>
                    </el-select>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item>
                <el-button @click="resetForm()">清除</el-button>
                <el-button type="primary" @click="getQuestionList"
                  >搜索</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <!-- tab栏切换 -->
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="全部" name="-1"></el-tab-pane>
          <el-tab-pane label="待审核" name="0"></el-tab-pane>
          <el-tab-pane label="已审核" name="1"></el-tab-pane>
          <el-tab-pane label="已拒绝" name="2"></el-tab-pane>
        </el-tabs>
        <el-alert
          :title="'数据一共' + informationList.counts + '条'"
          type="info"
          show-icon
          :closable="false"
        ></el-alert>
        <Lmtable :list="informationList" @choice="choice" :userHandle="userHandle"></Lmtable>
      </el-card>
    </div>
  </div>
</template>

<script>
import Lmtable from '../components/LM/LM-table.vue'
import { direction, difficulty, questionType } from '@/api/hmmm/constants.js'
import { provinces, citys } from '@/api/hmmm/citys.js'
import { simpleTwo, simple, choice, simpleTags, user } from '@/api/LM/Lm.js'
export default {
  components: {
    Lmtable
  },
  data () {
    return {
      activeName: '-1', // tab切换默认选择
      direction, // 引入方向
      difficulty, // 引入难度
      questionType, // 引入题型
      provinces: provinces(), // 引入城市
      citys: citys,
      cityList: [],
      labelPosition: 'right',
      form: {
        subjectID: '', // 科目
        catalogID: '', // 二级科目
        keyword: '', // 关键字
        tags: '', // 标签
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        creatorID: '', // 录入人
        remarks: '', // 题目标注
        shortName: '', // 企业简称
        province: '', // 省份
        city: '' // 城市
      },
      subject: [],
      directorys: [],
      labelTags: [], // 标签
      informationList: {},
      userInfor: [], // 录入人
      page: 1,
      pageSize: 5,
      chkState: '', // 表格状态
      userHandle: [], // 管理员处理结果
      checkSubmit: false
    }
  },
  created () {
    this.simple()
    this.choice()
    this.user()
  },
  methods: {
    toAddQuestion () {
      this.$router.push({
        name: 'questions-new-Z'
      })
    },
    async simple () {
      const data = await simple()
      this.subject = data.data
    },
    async changeSelect (val) {
      const param = { subjectID: val }
      const { data } = await simpleTwo(param)
      this.directorys = data
      const tags = await simpleTags(param)
      this.labelTags = tags.data
    },
    async choice (pageSize, pageNum) {
      this.page = pageNum || this.page
      this.pageSize = pageSize || this.pageSize
      const param = {
        page: this.page,
        pagesize: this.pageSize
      }
      if (this.chkState) {
        param.chkState = this.chkState
      }
      const { data } = await choice(param)
      this.informationList = data
    },
    async user () {
      const { data } = await user()
      this.userInfor = data
      // 处理数据
      this.userHandle = data.map(item => {
        return { value: item.id, label: item.username }
      })
    },
    resetForm () {
      // this.$refs.formName.resetFields()
      this.form = {
        subjectID: '', // 科目
        catalogID: '', // 二级科目
        keyword: '', // 关键字
        tags: '', // 标签
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        creatorID: '', // 录入人
        remarks: '', // 题目标注
        shortName: '', // 企业简称
        province: '', // 省份
        city: '' // 城市
      }
      delete this.form.page
      delete this.form.pagesize
      delete this.form.chkState
      this.checkSubmit = false
      this.choice()
    },
    // 二级城市
    cityChange (val) {
      this.form.city = ''
      this.cityList = citys(val)
    },
    // 搜索
    async getQuestionList () {
      if (this.objectValueAllEmpty(this.form)) {
        this.checkSubmit = false
        this.choice()
      } else {
        this.checkSubmit = true
        this.form.page = this.page
        this.form.pagesize = this.pageSize
        if (this.chkState) {
          this.form.chkState = this.chkState
        } else {
          delete this.form.chkState
        }
        const param = {}

        Object.keys(this.form).forEach(e => {
          if (this.form[e] != null & this.form[e] !== '') {
            param[e] = this.form[e]
          }
        })
        const { data } = await choice(param)
        this.informationList = data
      }
    },
    // 表格数据
    async handleClick (tab, event) {
      const chk = { chkState: tab.name }
      this.page = 1
      if (chk.chkState && tab.name !== '-1') {
        this.chkState = tab.name
      } else {
        this.chkState = ''
      }
      if (this.objectValueAllEmpty(this.form) || !this.checkSubmit) {
        this.choice()
      } else {
        this.getQuestionList()
      }
    },
    // 判断form的值是否为空
    objectValueAllEmpty (obj) {
      let isEmpty = true
      Object.keys(obj).forEach(e => {
        if (obj[e] != null & obj[e] !== '') {
          isEmpty = false
        }
      })
      return isEmpty
    }
  }
}
</script>

<style scoped lang="scss">
.top_title {
  margin-bottom: 15px;
  display: flex;
  justify-content: space-between;
  span {
    font-size: 12px;
    color: pink;
  }
}
</style>
